/**
 * Scalog - Scalog is a logging framework for the Scala programming language.
 * Copyright (C) 2009  Laurent Grangier
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.scalog.log4j

import org.apache.log4j.LogManager
import net.scalog.appender.Appender
import net.scalog.config.Configuration
import net.scalog.Level
import net.scalog.Logger
import java.io.InputStream
import org.apache.log4j.xml.DOMConfigurator
import javax.xml.parsers.DocumentBuilderFactory

/**
 * Implementation of Configuration for Log4J.
 *
 * @author Laurent Grangier
 */
class Log4JConfiguration extends Configuration {

  private val appenders = List(new Log4JAppender)

  def this(inputStream : InputStream) = {
    this();
    val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
    DOMConfigurator.configure(document.getDocumentElement);
  }
  
  def isEnabled(logger: Logger, level: Level): Boolean = {
    val log4jLogger = LogManager.getLogger(logger.name);
    log4jLogger.isEnabledFor(Log4JHelper.getLevel(level))
  }

  def getAppenders(logger: Logger): List[Appender] = appenders

}
